<template>
  <div class="hello">
    <el-dialog title="添加用户信息" :visible.sync="dialogAdd.show">
      <el-form :model="formDate" ref="formdong" label-width="100px" :rules="formrules">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formDate.name"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="formDate.author"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="formDate.price"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAdd.show = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormAdd('formdong')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'AddUser',
    props:{
      dialogAdd:''
    },
    data () {
      return {
        formDate:{
          name:'',
          author:'',
          price:''
        },
        formrules:{
          name:[{required:true,message:"用户名不能为空",trigger:"blur"}]
        }
      }
    },
    methods:{
      dialogFormAdd(formdong) {
        this.$refs[formdong].validate((valid) => {
          if (valid) {
            console.log(this.formDate.name,this.formDate.author,this.formDate.price)
            this.$axios.post('/api/books/',this.formDate).then(res => {
              console.log(this.formDate.name,this.formDate.author,this.formDate.price)
              this.$message({
                type:"success",
                message:"添加信息成功"
              })
              this.dialogAdd.show = false;
              this.$emit('update');
            })
            this.formDate  = {}
          } else {
            console.log('error submit!!');
            return false;
          }
        })
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>
